package com.eric.windows.viewAnimation;

import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
import android.widget.ViewSwitcher;

import com.eric.windows.R;

import java.util.ArrayList;

/**
 * 仿android Launcher 界面
 */
public class ViewSwitcherActivity extends Activity implements View.OnClickListener {

    private ViewSwitcher viewSwitcher;
    /** 每屏应用程序数 */
    public static final int NUMBER_PER_SCREEN = 12;
    private ArrayList<DataItem> items = new ArrayList<DataItem>();
    /** 当前显示的第几屏*/
    private int screenNo = -1;
    /** 屏幕总数*/
    private int screenCount = 0;
    private GridBaseAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewswitcher);

        viewSwitcher = (ViewSwitcher) findViewById(R.id.view_Switcher);
        findViewById(R.id.btn_pre).setOnClickListener(this);
        findViewById(R.id.btn_next).setOnClickListener(this);

        //添加数据
        for (int i = 0; i < 40; i++) {
            DataItem  item = new DataItem() ;
            item.dataName = "程序" + i;
            item.drawable = getDrawable(R.mipmap.ic_launcher);
            items.add(item);
        }
        //总屏幕数，items能被NUMBER_PER_SCREEN整除，结果就是总屏幕数， 否则总屏幕数就是结果 加 1
        screenCount = items.size() % NUMBER_PER_SCREEN == 0 ? items.size() / NUMBER_PER_SCREEN : items.size()/NUMBER_PER_SCREEN + 1;

        adapter = new GridBaseAdapter(screenNo,this,items,screenCount);

        viewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return LayoutInflater.from(ViewSwitcherActivity.this).inflate(R.layout.item_vswitcher,null);
            }
        });

        //显示第一页
        screenNo ++;
        viewSwitcher.setInAnimation(this,R.anim.left_enter);
        viewSwitcher.setOutAnimation(this,R.anim.left_exit);
        ( (GridView)viewSwitcher.getNextView()).setAdapter(adapter);
        viewSwitcher.showNext();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_pre:
                if (screenNo >0){
                    screenNo --;//记录当前屏幕
                    //添加XML动画，右进左出
                    viewSwitcher.setInAnimation(this,R.anim.right_enter);
                    viewSwitcher.setOutAnimation(this,R.anim.right_exit);
                    ( (GridView)viewSwitcher.getNextView()).setAdapter(adapter);
                    viewSwitcher.showPrevious();
                }
                break;
            case R.id.btn_next:
                if (screenNo <= screenCount){
                    screenNo ++;//记录当前屏幕
                    //添加XML动画，左进右出
                    viewSwitcher.setInAnimation(this,R.anim.left_enter);
                    viewSwitcher.setOutAnimation(this,R.anim.left_exit);
                    ( (GridView)viewSwitcher.getNextView()).setAdapter(adapter);
                    viewSwitcher.showNext();
                }
                break;
        }
    }
}
